package src

import (
//	"fmt"
	"net/http"
//    "appengine/datastore"
    "utils"
    "html/template"
    "strconv"
    "appengine"
    "appengine/memcache"
)

func init() {
	http.HandleFunc("/", homeHandler)
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	//Readme
	p := r.FormValue("p")
	var page int
	
	if (p != "") {
		page,_ = strconv.Atoi(p)
	} else {
		page = 1
	}
	
	nextpage := 0
	prevpage := 0 
	readmes := utils.ListReadme(r, page, 20)
	
	if (len(readmes) >= 10) {
		nextpage = page + 1
	}
	
	if (page > 1) {
		prevpage = page - 1
	}
	
	var recent = [] utils.Readme {}
  	memcache.Gob.Get(c, "recent", &recent)
	
	/*
	for _, v := range rs {
		fmt.Fprintln(w, v.Id)
	}*/
	
	t := template.Must(template.New("Template").ParseFiles("templates/pages/home.html", "templates/layouts/header.html", "templates/layouts/footer.html"))
	
	t.Execute(w, map[string]interface{}{"readmes" : readmes, "nextpage" : nextpage, "prevpage" : prevpage, "page" : page, "recent" : recent}) 
	
}